package com.yaozheng.vocationaltraining.service.impl.exam;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.exam.IExamHelpView;
import com.yaozheng.vocationaltraining.iview.exam.IPKView;
import com.yaozheng.vocationaltraining.service.exam.PKService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

/**
 * Created by moon on 2017/11/14.
 */

@EBean
public class PKServiceImpl implements PKService {
    IPKView iTokenView;
    @Override
    public void init(IPKView ipkView) {
        this.iTokenView = ipkView;
    }

    @Background
    @Override
    public void loadPKCategoryData() {
        try {
            OkHttpClientManager.getInstance().get(
                    Constants.ROOTURL + "/pk/categories",
                    iTokenView.getToken(), new BaseView(iTokenView) {
                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iTokenView.responseSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iTokenView.responseError(error);
                        }

                    });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenView.isResponseResult()) {
                iTokenView.responseError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }
        }
    }

    @Background
    @Override
    public void loadWinRate() {
        try {
            OkHttpClientManager.getInstance().get(
                    Constants.ROOTURL + "/pk/win-rate",
                    iTokenView.getToken(), new BaseView(iTokenView) {
                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iTokenView.winRateSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iTokenView.winRateError(error);
                        }

                    });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenView.isResponseResult()) {
                iTokenView.winRateError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }
        }
    }

    @Background
    @Override
    public void getUserInfo() {
        try {
            OkHttpClientManager
                    .getInstance()
                    .get(Constants.ROOTURL + "/user/info",
                            iTokenView.getToken(), new BaseView(iTokenView) {
                                @Override
                                public void responseSuccess(JSONObject jsonObject) {
                                    iTokenView.userInfoSuccess(jsonObject);
                                }

                                @Override
                                public void responseError(String error) {
                                    iTokenView.userInfoError(error);
                                }
                            });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenView.isResponseResult()) {
                iTokenView
                        .userInfoError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

    @Background
    @Override
    public void loadScore() {
        try {
            OkHttpClientManager
                    .getInstance()
                    .get(Constants.ROOTURL + "/user/score",
                            iTokenView.getToken(), new BaseView(iTokenView) {
                                @Override
                                public void responseSuccess(JSONObject jsonObject) {
                                    iTokenView.loadScoreSuccess(jsonObject);
                                }

                                @Override
                                public void responseError(String error) {
                                    iTokenView.loadScoreError(error);
                                }
                            });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenView.isResponseResult()) {
                iTokenView
                        .loadScoreError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }
}
